<?php
/**
 *用户地址管理控制器congcong
 */
namespace Home\Controller;
use Think\Controller;
class AddressController extends Controller{
    /**
     * 用户地址列表控制器
     */
    public function index()
    {    
       $dress=M('user_address')->order("isdefault desc")->where("uid='%d'",$_SESSION['uid'])->select();
       foreach($dress as $k=>$v)
       {
         $dress[$k]['shengid']=M('area')->where("region_id='%d'",$v['shengid'])->getField("region_name");
         $dress[$k]['shiid']=M('area')->where("region_id='%d'",$v['shiid'])->getField("region_name");
         if(empty($v['countyid']))
         {
           $dress[$k]['countyid']='';
         }else
         {
           $dress[$k]['countyid']=M('area')->where("region_id='%d'",$v['countyid'])->getField("region_name");
         }
       }
      
       $this->assign("dress",$dress);
       $this->display("");
    }
    /**
     * 删除收货地址
     */
    public function deldress()
    {
      $id=I("get.id",'','strip_tags');
      $res=M('user_address')->where("id='%d'",$id)->delete();
      if($res!==false)
      {
         echo "<script>alert('地址删除成功');location.href='/vshop.php/Home/Address/index'</script>";
      }else
      {
         echo "<script>alert('服务器故障');location.href='/vshop.php/Home/Address/index'</script>";
      }
    }
    /**
     * 新增收获地址
     */
    public function address()
    {
       ////获取省级区域
        $sheng=M('area')->where("parent_id=1")->getField("region_id,region_name");
        $this->assign("sheng",$sheng);
        $this->display();

    }
    /**
     * ajax获取区域
     */
    public function ajaxdress()
    {
        $where['parent_id']=I("get.id",0,'strip_tags');
        $str='';
        $res=M('area')->where($where)->getField("region_id,region_name");
        foreach($res as $k=>$v)
        {
           $str.= '<option value='.$k.'>'.$v.'</option>';
        }
        echo $str;

    }
    /**
     *保存收获地址
     */
    public function Savedress()
    { 
      $action=I('post.action','','strip_tags');////判断收货地址跳转
      $data['uid'] =!empty($_SESSION['uid'])?$_SESSION['uid']:0;
      $data['shengid']=I('post.shengid',0,'strip_tags');
      $data['shiid']=I('post.shiid',0,'strip_tags');
      $data['countyid']=I('post.countyid',0,'strip_tags');
      $data['address']=I('post.address','','strip_tags');
      $data['consignee']=I('post.consignee','','strip_tags');
      $data['phone']=I('post.phone','','strip_tags');
      $default=I('post.isdefault',0,'strip_tags'); 
       $data['isdefault']=$default;
      $id=M('user_address')->add($data);
       ///更改其他的默认状态st
      if(!empty($default))
      {
        $state=M('user_address')->where("uid='%d' and id!='%d'",$_SESSION['uid'],$id)->setField("isdefault",'0');
      }
     ///更改其他的默认状态end
      if($id>0 && empty($action))
      {
              echo "<script>alert('收货地址添加成功');location.href='/vshop.php/Home/Address/index'</script>";
      }elseif($id>0 && !empty($action))
      {
          echo "<script>alert('收货地址添加成功');location.href='/vshop.php/Home/Order/createOrder'</script>";
      }else
      {
              $this->error();
      }
    }
    /**
     * 编辑收货地址
     */
    public function editdress()
    {
      $where['id']=I('get.id','','strip_tags');
      $res=M('user_address')->where($where)->find();
          $sheng=M('area')->where("region_id='%d'",$res['shengid'])->getField("region_id,region_name");
          $shi=M('area')->where("region_id='%d'",$res['shiid'])->getField("region_id,region_name");

          if(empty($res['countyid']))
          {
              $county='';
          }else
          {
               $county=M('area')->where("region_id='%d'",$res['countyid'])->getField("region_id,region_name");
          }
     

          $all=M('area')->where("parent_id=1")->getField("region_id,region_name");
          $this->assign("sheng",$sheng)
          ->assign("dre",$res)
          ->assign('sheng',$sheng)
          ->assign('shi',$shi)
          ->assign('county',$county);
          $this->assign('all',$all);


         
          $this->display();
     
    }
    /**
     * 保存编辑的地址
     */
    public function Saveedit()
    {   $action=I('get.action','','strip_tags');////判断收货地址跳转
       $where['id']=I("post.id",'','strip_tags');
       $data['uid'] =$_SESSION['uid'];
      $data['shengid']=I('post.shengid',0,'strip_tags');
      $data['shiid']=I('post.shiid',0,'strip_tags');
      $data['countyid']=I('post.countyid',0,'strip_tags');
      $data['address']=I('post.address','','strip_tags');
      $data['consignee']=I('post.consignee','','strip_tags');
      $data['phone']=I('post.phone','','strip_tags');
      $default=I('post.isdefault',0,'strip_tags');
      $data['isdefault']=$default;
      ////修改默认地址
      if(!empty($default))
      {
         $state=M('user_address')->where("uid='%d' and id!='%d'",$_SESSION['uid'],$where['id'])->setField("isdefault",'0');
      }
      $res=M('user_address')->where($where)->data($data)->save();
      if($res!==false && empty($action))
      {
       
        echo "<script>alert('地址修改成功');location.href='/vshop.php/Home/Address/index'</script>";

      }elseif($res!==false && !empty($action))
      {
           echo "<script>alert('地址修改成功');location.href='/vshop.php/Home/Order/createOrder'</script>";
      }else{
        $this->error();
      }
     
      

    }
    /**
     * 
     */
    function area()
    {
    $where['parent_id']=I('get.id','','strip_tags');
    $result=M('area')->where($where)->getField("region_id,region_name");
    $str='';
    foreach($result as $k=>$v){
      $str.= '<option value='.$k.'>'.$v.'</option>';

    }
    echo $str;

  }

}
